// pages/complaintList/complaintList.js
const {
  rootPath,
  API,
  HomePath
} = require("../../utils/const")
const {
  postLogin,
  getRequest,
  requestUserInfo
} = require("../../utils/request")

const dateUtils = require("../../utils/dateUtils")

Page({

  /**
   * Page initial data
   */
  data: {
    complaints: []
  },

  /**
   * Lifecycle function--Called when page load
   */
  onLoad(options) {
    const that = this;
    // 发起请求，获取数据
    var user = wx.getStorageSync('userInfo');
    var patientId = user.userId;
    getRequest(API.getComplaints + '/' + patientId).then(res => {
      that.setData({
        complaints: res
      }, () => {
        const complaints = that.data.complaints;
        complaints.forEach(element => {
          element.complaintTime = dateUtils.formatComplaintTime(element.complaintTime);
        });

        that.setData({
          complaints: complaints
        })
      });
    });
  },

  viewDetail: function(e) {
    const complaintId = e.currentTarget.dataset.id;
    // 跳转到详情页面，并传递投诉的 ID
    wx.navigateTo({
      url: `/pages/complaintDetail/complaintDetail?id=${complaintId}`
    });
  },

  /**
   * Lifecycle function--Called when page is initially rendered
   */
  onReady() {

  },

  /**
   * Lifecycle function--Called when page show
   */
  onShow() {

  },

  /**
   * Lifecycle function--Called when page hide
   */
  onHide() {

  },

  /**
   * Lifecycle function--Called when page unload
   */
  onUnload() {

  },

  /**
   * Page event handler function--Called when user drop down
   */
  onPullDownRefresh() {

  },

  /**
   * Called when page reach bottom
   */
  onReachBottom() {

  },

  /**
   * Called when user click on the top right corner to share
   */
  onShareAppMessage() {

  }
})